package com.mxx.common.netty.http;


import com.mxx.common.netty.annotation.Http;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/6/29 11:15
 * @Description: 暂无描述
 */

public class NettyPath {
    private Http annotation;
    private String method;
    private String uri;
    private boolean equal;

    public static NettyPath make(String contextPath, Http annotation) {
        return new NettyPath(contextPath, annotation);
    }


    public NettyPath(String contextPath, Http annotation) {
        this.annotation = annotation;
        this.method = annotation.method().name();
        this.uri = String.format("/%s/%s", contextPath, annotation.path().toLowerCase());
        while ((uri.indexOf("//")) != -1) {
            this.uri = uri.replace("//", "/");
        }
        this.equal = annotation.equal();
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public boolean isEqual() {
        return equal;
    }

    public void setEqual(boolean equal) {
        this.equal = equal;
    }

    public Http getAnnotation() {
        return annotation;
    }


    @Override
    public String toString() {
        return method.toUpperCase() + " " + uri;
    }

    @Override
    public int hashCode() {
        return ("HTTP " + method.toUpperCase() + " " + uri.toUpperCase()).hashCode();
    }

    @Override
    public boolean equals(Object object) {
        if (object instanceof NettyPath) {
            NettyPath path = (NettyPath) object;
            return method.equalsIgnoreCase(path.method) && uri.equalsIgnoreCase(path.uri);
        }
        return false;
    }
}